package cn.hl.hm.controller;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.hl.hm.dao.VehicleMapper;
import cn.hl.hm.entity.Vehicle;
import cn.hl.hm.util.CheckVehicle;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/checkVehicleController")
public class CheckVehicleController {
	private static final Logger logger = LoggerFactory.getLogger(CheckVehicleController.class);
	private final static SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
	@Autowired
	VehicleMapper vehicleMapper;

	/**
	 * 批量验证车牌信息 并批量更新
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/CheckVehicle", method = RequestMethod.POST)
	public String CheckVehicle(HttpServletRequest request, HttpServletResponse response) throws IOException {
		logger.info("批量验证车辆信息");
		request.setCharacterEncoding("UTF-8");
		List<Vehicle> vL = vehicleMapper.selectInfo(null);
		for (Vehicle vehicle : vL) {
			// 新
			if (vehicle.getPeriodTime() != null && vehicle.getRoadTransportCertificateNumber()!=null) {
				// 比较日期 ，判断是否过期
				if (vehicle.getPeriodTime().before(new Date())) {
					// 时间过期，再次调用接口验证是否更新
					Date result = CheckVehicle.checkJieRuReturnDate(vehicle.getPlateNumber());
					Vehicle updateVehicle = new Vehicle();
					updateVehicle.setId(vehicle.getId());
					if (result != null) {
						updateVehicle.setPeriodTime(result);
						logger.info(vehicle.getPlateNumber() + " 道路许可证过期或车辆异常");
						if (result.before(new Date())) {
							updateVehicle.setRtciStatus(1);
						} else { 
							updateVehicle.setRtciStatus(0);
						}
					} else { // 返回为null 设置为1
						updateVehicle.setRtciStatus(1);
					}
					vehicleMapper.updateByPrimaryKeySelective(updateVehicle);
				}
			} else { // 如果日期不存在(第一次调用时)，则调用接口设置日期进行验证
				Vehicle updateVehicle = new Vehicle();
				updateVehicle.setId(vehicle.getId());
				JSONObject jsonObject = CheckVehicle.checkJieRuReturnCarInfo(vehicle.getPlateNumber());
				if (jsonObject != null) { // 日期过期设置为1
					logger.info(vehicle.getPlateNumber() + "设置过期日期");
					// 获取过期日期
//					// 比较日期 ，判断是否过期
					try {
						Date result = shortSdf.parse(jsonObject.get("PeriodEndDate").toString());
						updateVehicle.setPeriodTime(result);
						if (result.before(new Date())) {
							updateVehicle.setRtciStatus(1);
						}
						// 设置道路许可证
						updateVehicle.setRoadTransportCertificateNumber(jsonObject.get("RoadTransportCertificateNumber").toString());
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} 

					
				} else { // 返回为null 设置为1
					updateVehicle.setRtciStatus(1);
				}
				vehicleMapper.updateByPrimaryKeySelective(updateVehicle);
			}
			int rw = CheckVehicle.checkRuWang(vehicle.getPlateNumber());
			Vehicle updateVehicle = new Vehicle();
			updateVehicle.setId(vehicle.getId());
			if (rw == 0) { // 未入网
				logger.info(vehicle.getPlateNumber() + " 车辆未入网");
				updateVehicle.setRwStatus(1);
			}else {
				updateVehicle.setRwStatus(0);
			}
			vehicleMapper.updateByPrimaryKeySelective(updateVehicle);
			// 旧 直接调用接口验证
			// int result = CheckVehicle.checkJieRu(vehicle.getPlateNumber());
			// if (result == 1) { // 道路许可证过期
			// Vehicle updateVehicle = new Vehicle();
			// updateVehicle.setId(vehicle.getId());
			// logger.info(vehicle.getPlateNumber() + " 道路许可证过期或车辆异常");
			// updateVehicle.setRtciStatus(1);
			// vehicleMapper.updateByPrimaryKeySelective(updateVehicle);
			//
			// }
			// int rw = CheckVehicle.checkRuWang(vehicle.getPlateNumber());
			// if (rw == 0) { // 未入网
			// Vehicle updateVehicle = new Vehicle();
			// updateVehicle.setId(vehicle.getId());
			// logger.info(vehicle.getPlateNumber() + " 车辆未入网");
			// updateVehicle.setRwStatus(1);
			// vehicleMapper.updateByPrimaryKeySelective(updateVehicle);
			// }
		}
		return null;
	}

	/**
	 * 根据车牌判断是否入网 道路许可证是否过期
	 * 
	 * @param request
	 * @param response
	 * @param plate
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/CheckVehicleByPlate", method = RequestMethod.POST)
	public String CheckVehicleByPlate(HttpServletRequest request, HttpServletResponse response, String plate)
			throws IOException {

		request.setCharacterEncoding("UTF-8");
		int result = CheckVehicle.checkJieRu(plate);
		if (result == 1) { // 道路许可证过期
			System.out.println(" 道路许可证过期或车辆异常");
		} else {
			System.out.println(" 道路许可证过期正常");
		}
		int rw = CheckVehicle.checkRuWang(plate);
		if (rw == 0) { // 未入网
			System.out.println(" 车辆未入网");

		} else {
			System.out.println("车辆入网正常");
		}

		return null;
	}

	/**
	 * 批量设置车辆过期日期
	 * 
	 * @param request
	 * @param response
	 * @param plate
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/setPeriodDate", method = RequestMethod.POST)
	public String setPeriodDate(HttpServletRequest request, HttpServletResponse response, String plate)
			throws IOException {

		logger.info("设置车辆过期时间");
		request.setCharacterEncoding("UTF-8");
		List<Vehicle> vL = vehicleMapper.selectInfo(null);

		for (Vehicle vehicle : vL) {
			logger.info("设置车辆过期时间" + vehicle.getPlateNumber());

			Date result = CheckVehicle.checkJieRuReturnDate(vehicle.getPlateNumber());
			if (result != null) {
				Vehicle updateVehicle = new Vehicle();
				updateVehicle.setId(vehicle.getId());
				updateVehicle.setPeriodTime(result);
				vehicleMapper.updateByPrimaryKeySelective(updateVehicle);
			}

		}
		return null;
	}

}
